package com.qf.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import javax.management.Query;
import java.util.Date;

@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    private DtsStorageMapper storageMapper;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Override
    public IPage<DtsStorage> queryStoragePage(String key, String name, Integer page, Integer limit, String sort, String order) {

        //创建分页对象
        IPage<DtsStorage> ipage = new Page<>(page, limit);

        //封装查询对象
        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("key", key);
        }
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(sort);
            } else {
                queryWrapper.orderByDesc(sort);
            }
        }



        IPage<DtsStorage> resultPage = storageMapper.selectPage(ipage, queryWrapper);

        return resultPage;
    }

    @Override
    public DtsStorage add(String filename, int size, String ext, byte[] content)  throws  Exception{
        //1. 生成随机字符串, 作为文件名, 防止文件重名
        String key = UUID.randomUUID().toString();
        //2. 用随机生成的字符串 + 文件扩展名组成新的文件名, 防止文件重名
        String fileName = key+ext;
        //3. 调用阿里云工具类, 保存文件到阿里云oss服务器, 返回保存路径
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, fileName, content);
        //4. 创建Storage实体类对象, 封装属性
        DtsStorage storage = new DtsStorage();

        storage.setDeleted(false);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setKey(fileName);
        storage.setName(filename);
        storage.setType(ext);
        storage.setSize(size);
        storage.setUrl(url);

        storageMapper.insert(storage);
        return storage;
    }

    @Override
    public int update(DtsStorage dtsStorage) {
        dtsStorage.setUpdateTime(new Date());

        int count = storageMapper.updateById(dtsStorage);
        return count;
    }

    @Override
    public void deleteByKey(String key) {
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,key);

        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("`key`",key);
        storageMapper.delete(queryWrapper);


    }

}
